package com.techysky.util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.util.Enumeration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Demo1 {
	private static Log log=LogFactory.getLog(Demo1.class);
	
	public static void main(String[] args) throws IOException {
		InetAddress ina;
		try {
			ina = InetAddress.getLocalHost();
			System.out.println(ina);
			System.out.println(ina.getAddress());// 返回此 InetAddress 对象的原始 IP地址
	
			//设备Ip地址
			System.out.println(ina.getHostAddress());
			 //检查连接状态
			//String ip11="172.16.14.105";
		    InetAddress byName = InetAddress.getByName(ina.getHostAddress());
		    System.out.println(byName.isReachable(1000));
			
		
			Enumeration<NetworkInterface> netInterfaces = null;   
			try {   
			    netInterfaces = NetworkInterface.getNetworkInterfaces();   
			    while (netInterfaces.hasMoreElements()) {   
			        NetworkInterface ni = netInterfaces.nextElement();   
			        System.out.println("DisplayName:" + ni.getDisplayName());   
			        System.out.println("Name:" + ni.getName());   
			        Enumeration<InetAddress> ips = ni.getInetAddresses();   
			        while (ips.hasMoreElements()) {   
			            System.out.println("IP:"  
			            + ips.nextElement().getAddress());   
			        }   
			    }   
			} catch (Exception e) {   
			    log.info(e); 
			}    
		} 
		catch (Exception e) {
			log.info(e); 
		}


		
	}
	//检查ip地址链接是否正常
	public static boolean isHostConnectable(String host, int port) throws IOException{
		Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(host, port));
            socket.close();
        } catch (IOException e) {
        	log.info(e); 
            return false;
        } finally {
        	socket.close();
        }
        return true;
    }
}